{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "from ftrlp import *\n",
    "from sklearn.metrics import log_loss as log_loss_sk\n",
    "from sklearn.model_selection import train_test_split"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "fe_gbdt_path = \"data/FE_gbdt_data.csv\"\n",
    "\n",
    "fe_train_path = \"data/FE_train_data.csv\"\n",
    "fe_test_path = \"data/FE_test_data.csv\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#将处理后的数据切分为训练集和测试集\n",
    "all_data = pd.read_csv(fe_gbdt_path)\n",
    "X_col = [i for i in all_data.columns if i not in [\"click\"]]\n",
    "X = all_data[X_col]\n",
    "y = all_data[\"click\"]\n",
    "\n",
    "X_train, X_val, y_train, y_val = train_test_split(X, y, test_size = 0.2, random_state = 10)\n",
    "\n",
    "fe_train_data = pd.concat([X_train, y_train], axis=1)\n",
    "fe_test_data = pd.concat([X_val, y_val], axis=1)\n",
    "\n",
    "fe_train_data.to_csv(fe_train_path, index=False)\n",
    "fe_test_data.to_csv(fe_test_path, index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def get_features_list():\n",
    "    data = pd.read_csv(fe_test_path)\n",
    "    col = [x for x in data.columns  if x not in ['id', 'click']]\n",
    "    return col"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "features num = 12644\n"
     ]
    }
   ],
   "source": [
    "features = get_features_list()\n",
    "max_features = len(features)\n",
    "print \"features num = %s\" % max_features\n",
    "target = \"click\"\n",
    "descriptive = [\"id\"]\n",
    "categorical = []\n",
    "numerical = features\n",
    "\n",
    "data_gen = DataGen(max_features = max_features*2, target = target, descriptive = descriptive, \\\n",
    "                   categorical = categorical, numerical = numerical)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "data_path = fe_train_path\n",
    "test_path = fe_test_path\n",
    "\n",
    "test_data = fe_test_data\n",
    "y = test_data[target]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "alpha=1, beta=1, l1=1, l2=0.5, subsample=0.8, rate=20000000, epoch=50\n",
      "TRAINING EPOCH:  1\n",
      "------------------\n",
      "EPOCH  1 FINISHED IN 513 seconds\n",
      "\n",
      "TRAINING EPOCH:  2\n",
      "------------------\n",
      "EPOCH  2 FINISHED IN 531 seconds\n",
      "\n",
      "TRAINING EPOCH:  3\n",
      "------------------\n",
      "EPOCH  3 FINISHED IN 552 seconds\n",
      "\n",
      "TRAINING EPOCH:  4\n",
      "------------------\n",
      "EPOCH  4 FINISHED IN 568 seconds\n",
      "\n",
      "TRAINING EPOCH:  5\n",
      "------------------\n",
      "EPOCH  5 FINISHED IN 574 seconds\n",
      "\n",
      "TRAINING EPOCH:  6\n",
      "------------------\n",
      "EPOCH  6 FINISHED IN 579 seconds\n",
      "\n",
      "TRAINING EPOCH:  7\n",
      "------------------\n",
      "EPOCH  7 FINISHED IN 589 seconds\n",
      "\n",
      "TRAINING EPOCH:  8\n",
      "------------------\n",
      "EPOCH  8 FINISHED IN 594 seconds\n",
      "\n",
      "TRAINING EPOCH:  9\n",
      "------------------\n",
      "EPOCH  9 FINISHED IN 598 seconds\n",
      "\n",
      "TRAINING EPOCH: 10\n",
      "------------------\n",
      "EPOCH 10 FINISHED IN 605 seconds\n",
      "\n",
      "TRAINING EPOCH: 11\n",
      "------------------\n",
      "EPOCH 11 FINISHED IN 610 seconds\n",
      "\n",
      "TRAINING EPOCH: 12\n",
      "------------------\n",
      "EPOCH 12 FINISHED IN 611 seconds\n",
      "\n",
      "TRAINING EPOCH: 13\n",
      "------------------\n",
      "EPOCH 13 FINISHED IN 613 seconds\n",
      "\n",
      "TRAINING EPOCH: 14\n",
      "------------------\n",
      "EPOCH 14 FINISHED IN 616 seconds\n",
      "\n",
      "TRAINING EPOCH: 15\n",
      "------------------\n",
      "EPOCH 15 FINISHED IN 622 seconds\n",
      "\n",
      "TRAINING EPOCH: 16\n",
      "------------------\n",
      "EPOCH 16 FINISHED IN 620 seconds\n",
      "\n",
      "TRAINING EPOCH: 17\n",
      "------------------\n",
      "EPOCH 17 FINISHED IN 623 seconds\n",
      "\n",
      "TRAINING EPOCH: 18\n",
      "------------------\n",
      "EPOCH 18 FINISHED IN 620 seconds\n",
      "\n",
      "TRAINING EPOCH: 19\n",
      "------------------\n",
      "EPOCH 19 FINISHED IN 629 seconds\n",
      "\n",
      "TRAINING EPOCH: 20\n",
      "------------------\n",
      "EPOCH 20 FINISHED IN 623 seconds\n",
      "\n",
      "TRAINING EPOCH: 21\n",
      "------------------\n",
      "EPOCH 21 FINISHED IN 620 seconds\n",
      "\n",
      "TRAINING EPOCH: 22\n",
      "------------------\n",
      "EPOCH 22 FINISHED IN 624 seconds\n",
      "\n",
      "TRAINING EPOCH: 23\n",
      "------------------\n",
      "EPOCH 23 FINISHED IN 623 seconds\n",
      "\n",
      "TRAINING EPOCH: 24\n",
      "------------------\n",
      "EPOCH 24 FINISHED IN 626 seconds\n",
      "\n",
      "TRAINING EPOCH: 25\n",
      "------------------\n",
      "EPOCH 25 FINISHED IN 626 seconds\n",
      "\n",
      "TRAINING EPOCH: 26\n",
      "------------------\n",
      "EPOCH 26 FINISHED IN 625 seconds\n",
      "\n",
      "TRAINING EPOCH: 27\n",
      "------------------\n",
      "EPOCH 27 FINISHED IN 627 seconds\n",
      "\n",
      "TRAINING EPOCH: 28\n",
      "------------------\n",
      "EPOCH 28 FINISHED IN 630 seconds\n",
      "\n",
      "TRAINING EPOCH: 29\n",
      "------------------\n",
      "EPOCH 29 FINISHED IN 631 seconds\n",
      "\n",
      "TRAINING EPOCH: 30\n",
      "------------------\n",
      "EPOCH 30 FINISHED IN 630 seconds\n",
      "\n",
      "TRAINING EPOCH: 31\n",
      "------------------\n",
      "EPOCH 31 FINISHED IN 631 seconds\n",
      "\n",
      "TRAINING EPOCH: 32\n",
      "------------------\n",
      "EPOCH 32 FINISHED IN 627 seconds\n",
      "\n",
      "TRAINING EPOCH: 33\n",
      "------------------\n",
      "EPOCH 33 FINISHED IN 630 seconds\n",
      "\n",
      "TRAINING EPOCH: 34\n",
      "------------------\n",
      "EPOCH 34 FINISHED IN 632 seconds\n",
      "\n",
      "TRAINING EPOCH: 35\n",
      "------------------\n",
      "EPOCH 35 FINISHED IN 631 seconds\n",
      "\n",
      "TRAINING EPOCH: 36\n",
      "------------------\n",
      "EPOCH 36 FINISHED IN 631 seconds\n",
      "\n",
      "TRAINING EPOCH: 37\n",
      "------------------\n",
      "EPOCH 37 FINISHED IN 635 seconds\n",
      "\n",
      "TRAINING EPOCH: 38\n",
      "------------------\n",
      "EPOCH 38 FINISHED IN 639 seconds\n",
      "\n",
      "TRAINING EPOCH: 39\n",
      "------------------\n",
      "EPOCH 39 FINISHED IN 639 seconds\n",
      "\n",
      "TRAINING EPOCH: 40\n",
      "------------------\n",
      "EPOCH 40 FINISHED IN 638 seconds\n",
      "\n",
      "TRAINING EPOCH: 41\n",
      "------------------\n",
      "EPOCH 41 FINISHED IN 639 seconds\n",
      "\n",
      "TRAINING EPOCH: 42\n",
      "------------------\n",
      "EPOCH 42 FINISHED IN 636 seconds\n",
      "\n",
      "TRAINING EPOCH: 43\n",
      "------------------\n",
      "EPOCH 43 FINISHED IN 641 seconds\n",
      "\n",
      "TRAINING EPOCH: 44\n",
      "------------------\n",
      "EPOCH 44 FINISHED IN 635 seconds\n",
      "\n",
      "TRAINING EPOCH: 45\n",
      "------------------\n",
      "EPOCH 45 FINISHED IN 642 seconds\n",
      "\n",
      "TRAINING EPOCH: 46\n",
      "------------------\n",
      "EPOCH 46 FINISHED IN 634 seconds\n",
      "\n",
      "TRAINING EPOCH: 47\n",
      "------------------\n",
      "EPOCH 47 FINISHED IN 636 seconds\n",
      "\n",
      "TRAINING EPOCH: 48\n",
      "------------------\n",
      "EPOCH 48 FINISHED IN 634 seconds\n",
      "\n",
      "TRAINING EPOCH: 49\n",
      "------------------\n",
      "EPOCH 49 FINISHED IN 640 seconds\n",
      "\n",
      "TRAINING EPOCH: 50\n",
      "------------------\n",
      "EPOCH 50 FINISHED IN 637 seconds\n",
      "\n",
      " --- TRAINING FINISHED IN 30886 SECONDS WITH LOSS 45677.47 ---\n",
      "\n",
      "test set log loss = 9.73395127578\n"
     ]
    }
   ],
   "source": [
    "alpha = 1\n",
    "beta = 1\n",
    "l1 = 1\n",
    "l2 = 0.5\n",
    "subsample = 0.8\n",
    "rate = 20000000\n",
    "epoch = 50\n",
    "\n",
    "print(\"alpha=%s, beta=%s, l1=%s, l2=%s, subsample=%s, rate=%s, epoch=%s\" % (alpha, beta, l1, l2, subsample, rate, epoch))\n",
    "ftrlp = FTRLP(alpha=alpha, beta=beta, l1=l1, l2=l2, subsample=subsample, epochs=epoch, rate=rate)\n",
    "ftrlp.partial_fit(data_gen, data_path)\n",
    "y_pred = ftrlp.predict(data_gen, test_path)\n",
    "logloss = log_loss_sk(y, y_pred)\n",
    "print(\"test set log loss = %s\" % (logloss))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
